/*
 * This confidential and proprietary software may be used only as
 * authorised by a licensing agreement from ARM Limited
 * (C) COPYRIGHT 2014-2016, 2018-2021 ARM Limited
 * ALL RIGHTS RESERVED
 * The entire notice above must be reproduced on all authorised
 * copies and copies may only be made to the extent permitted
 * by a licensing agreement from ARM Limited.
 */
#ifndef _MALI_FBDEV_TYPES_H_
#define _MALI_FBDEV_TYPES_H_

/*
 * The following #defines are deprecated. Please set egl_linux_pixmap.pixmap_format
 * to EGL_LEGACY_PIXMAP_FORMAT_INVALID and use the egl_linux_pixmap.drm_fourcc
 * fields instead, providing a DRM_FORMAT and (optionally) a DRM_FORMAT_MOD_ARM_AFBC
 * modifier.
 */
#define EGL_LEGACY_PIXMAP_FORMAT_INVALID 0x0000000000000000
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565 0x0000000001040A88
#define EGL_LEGACY_PIXMAP_FORMAT_RGB565 0x0000000001040A0A
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888 0x00000000010BB688
#define EGL_LEGACY_PIXMAP_FORMAT_ARGB8888 0x00000000010BB60A
#define EGL_LEGACY_PIXMAP_FORMAT_BGRA8888 0x00000000010BB0D1
#define EGL_LEGACY_PIXMAP_FORMAT_RGBA8888 0x00000000010BB053
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888 0x00000000010BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_XRGB8888 0x00000000010BBA0A
#define EGL_LEGACY_PIXMAP_FORMAT_BGRX8888 0x00000000010BBAD1
#define EGL_LEGACY_PIXMAP_FORMAT_RGBX8888 0x00000000010BBA53
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888 0x00000000010B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_RGB888 0x00000000010B3A0A
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR4444 0x00000000010BA688
#define EGL_LEGACY_PIXMAP_FORMAT_ARGB4444 0x00000000010BA60A
#define EGL_LEGACY_PIXMAP_FORMAT_BGRA4444 0x00000000010BA0D1
#define EGL_LEGACY_PIXMAP_FORMAT_RGBA4444 0x00000000010BA053
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR1555 0x0000000001041688
#define EGL_LEGACY_PIXMAP_FORMAT_ARGB1555 0x000000000104160A
#define EGL_LEGACY_PIXMAP_FORMAT_BGRA5551 0x000000000104260A
#define EGL_LEGACY_PIXMAP_FORMAT_RGBA5551 0x0000000001042688
#define EGL_LEGACY_PIXMAP_FORMAT_L8 0x00000000010A3A00
#define EGL_LEGACY_PIXMAP_FORMAT_R8 0x00000000010A3B20
#define EGL_LEGACY_PIXMAP_FORMAT_YV12_BT601_NARROW 0x0000000001027A88
#define EGL_LEGACY_PIXMAP_FORMAT_YV12_BT601_WIDE 0x0000000401027A88
#define EGL_LEGACY_PIXMAP_FORMAT_YV12_BT709_NARROW 0x0000000801027A88
#define EGL_LEGACY_PIXMAP_FORMAT_YV12_BT709_WIDE 0x0000000C01027A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV12_BT601_NARROW 0x0000000001026A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV12_BT601_WIDE 0x0000000401026A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV12_BT709_NARROW 0x0000000801026A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV12_BT709_WIDE 0x0000000C01026A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV16_BT601_NARROW 0x0000000001032A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV16_BT601_WIDE 0x0000000401032A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV16_BT709_NARROW 0x0000000801032A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV16_BT709_WIDE 0x0000000C01032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUYV_BT601_NARROW 0x0000000001022A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUYV_BT601_WIDE 0x0000000401022A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUYV_BT709_NARROW 0x0000000801022A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUYV_BT709_WIDE 0x0000000C01022A88
#define EGL_LEGACY_PIXMAP_FORMAT_NV21_BT601_NARROW 0x0000000001026A50
#define EGL_LEGACY_PIXMAP_FORMAT_NV21_BT709_NARROW 0x0000000801026A50
#define EGL_LEGACY_PIXMAP_FORMAT_NV21_BT709_WIDE 0x0000000C01026A50
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888_AFBC 0x00000000360BB688
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888_AFBC 0x00000000360BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888_AFBC 0x00000000360B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565_AFBC 0x0000000036040A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR1555_AFBC 0x0000000036041688
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR4444_AFBC 0x00000000360BA688
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888_AFBC_TILED 0x00000001360BB688
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888_AFBC_TILED 0x00000001360BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888_AFBC_TILED 0x00000001360B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565_AFBC_TILED 0x0000000136040A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR1555_AFBC_TILED 0x0000000136041688
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR4444_AFBC_TILED 0x00000001360BA688
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_NARROW_AFBC 0x0000000016026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_WIDE_AFBC 0x0000000416026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_NARROW_AFBC 0x0000000816026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_WIDE_AFBC 0x0000000C16026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_NARROW_AFBC 0x0000000016032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_WIDE_AFBC 0x0000000416032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_NARROW_AFBC 0x0000000816032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_WIDE_AFBC 0x0000000C16032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_NARROW_AFBC_TILED 0x0000000116026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_WIDE_AFBC_TILED 0x0000000516026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_NARROW_AFBC_TILED 0x0000000916026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_WIDE_AFBC_TILED 0x0000000D16026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_NARROW_AFBC_TILED 0x0000000116032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_WIDE_AFBC_TILED 0x0000000516032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_NARROW_AFBC_TILED 0x0000000916032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_WIDE_AFBC_TILED 0x0000000D16032A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888_AFBC_SPLITBLK 0x00000000760BB688
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888_AFBC_SPLITBLK 0x00000000760BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888_AFBC_SPLITBLK 0x00000000760B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565_AFBC_SPLITBLK 0x0000000076040A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888_AFBC_SPLITBLK_TILED 0x00000001760BB688
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888_AFBC_SPLITBLK_TILED 0x00000001760BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888_AFBC_SPLITBLK_TILED 0x00000001760B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565_AFBC_SPLITBLK_TILED 0x0000000176040A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888_AFBC_SPLITBLK_WIDEBLK 0x00000000F60BB688
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888_AFBC_SPLITBLK_WIDEBLK 0x00000000F60BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888_AFBC_SPLITBLK_WIDEBLK 0x00000000F60B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565_AFBC_WIDEBLK 0x00000000B6040A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR8888_AFBC_SPLITBLK_WIDEBLK_TILED 0x00000001F60BB688
#define EGL_LEGACY_PIXMAP_FORMAT_XBGR8888_AFBC_SPLITBLK_WIDEBLK_TILED 0x00000001F60BBA88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR888_AFBC_SPLITBLK_WIDEBLK_TILED 0x00000001F60B3A88
#define EGL_LEGACY_PIXMAP_FORMAT_BGR565_AFBC_WIDEBLK_TILED 0x00000001B6040A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_NARROW_AFBC_SPLITBLK 0x0000000056026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_WIDE_AFBC_SPLITBLK 0x0000000456026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_NARROW_AFBC_SPLITBLK 0x0000000856026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_WIDE_AFBC_SPLITBLK 0x0000000C56026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_NARROW_AFBC_SPLITBLK_TILED 0x0000000156026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_WIDE_AFBC_SPLITBLK_TILED 0x0000000556026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_NARROW_AFBC_SPLITBLK_TILED 0x0000000956026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_WIDE_AFBC_SPLITBLK_TILED 0x0000000D56026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_NARROW_AFBC_WIDEBLK 0x0000000096026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_WIDE_AFBC_WIDEBLK 0x0000000496026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_NARROW_AFBC_WIDEBLK 0x0000000896026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_WIDE_AFBC_WIDEBLK 0x0000000C96026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_NARROW_AFBC_WIDEBLK 0x0000000096032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_WIDE_AFBC_WIDEBLK 0x0000000496032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_NARROW_AFBC_WIDEBLK 0x0000000896032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_WIDE_AFBC_WIDEBLK 0x0000000C96032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_NARROW_AFBC_WIDEBLK_TILED 0x0000000196026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT601_WIDE_AFBC_WIDEBLK_TILED 0x0000000596026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_NARROW_AFBC_WIDEBLK_TILED 0x0000000996026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_8BIT_BT709_WIDE_AFBC_WIDEBLK_TILED 0x0000000D96026A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_NARROW_AFBC_WIDEBLK_TILED 0x0000000196032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT601_WIDE_AFBC_WIDEBLK_TILED 0x0000000596032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_NARROW_AFBC_WIDEBLK_TILED 0x0000000996032A88
#define EGL_LEGACY_PIXMAP_FORMAT_YUV422_8BIT_BT709_WIDE_AFBC_WIDEBLK_TILED 0x0000000D96032A88
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR2101010 0x0000000001043688
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR2101010_AFBC 0x0000000011043688
#define EGL_LEGACY_PIXMAP_FORMAT_ABGR2101010_AFBC_TILED 0x0000000111043688
#define EGL_LEGACY_PIXMAP_FORMAT_Y0L2 0x000001000102A690
#define EGL_LEGACY_PIXMAP_FORMAT_P010 0x0000010001036690
#define EGL_LEGACY_PIXMAP_FORMAT_P210 0x0000010001034290
#define EGL_LEGACY_PIXMAP_FORMAT_Y210 0x0000010001032290
#define EGL_LEGACY_PIXMAP_FORMAT_Y410 0x0000010001028092
#define EGL_LEGACY_PIXMAP_FORMAT_Y16_U16V16_420 0x0000020001037690
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_10BIT_AFBC 0x000001001602A690
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_10BIT_AFBC_WIDEBLK 0x000001009602A690
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_10BIT_AFBC_TILED 0x000001011602A690
#define EGL_LEGACY_PIXMAP_FORMAT_YUV420_10BIT_AFBC_WIDEBLK_TILED 0x000001019602A690

typedef struct fbdev_window
{
	unsigned short width;
	unsigned short height;
} fbdev_window;

typedef union mem_handle
{
	int fd;                    /* fd to dma memory */
} mem_handle;

typedef struct egl_linux_pixmap
{
	int width, height;

	struct
	{
		/** @brief The average bytes stride of each plane.
		 * For most formats, the stride is the number of bytes between the subsequent rows of samples in the planes.
		 * For block formats, it is the number of bytes from one row of blocks to the next divided by block height of the plane.
		 * For each plane required by the format, the stride must not be negative value. Other entries in
		 * the array should be 0.
		 */
		uint32_t stride;
		/** @brief The byte size of each plane.
		 * For each plane required by the format, the number of bytes taken up by that plane. That includes any space wasted
		 * in partially-used blocks.
		 */
		uint32_t size;
		/** @brief The offset from the memory handle to each plane.
		 * For each plane required by the format, the number of bytes from the start of the external memory region to the start of that
		 * plane. Other entries in the array should be 0.
		 */
		uint32_t offset;
	}
	planes[3];

	/**
	 * @brief An EGL_LEGACY_PIXMAP_FORMAT value that specifies the format of the pixmap
	 *
	 * When specifying a drm_fourcc format, pixmap_format must be 0.
	 */
	uint64_t pixmap_format;

	/** Three buffers that can be used by this pixmap. In case of RGB pixmap only the first one is going to be
	 * used. In case of YUV pixmap all three of them can be used for storing Y, U and V color coordinates separately.*/
	mem_handle handles[3];

	/** When pixmap_format = 0 then this drm_fourcc format specification will be used. */
	struct
	{
		uint32_t format;
		uint64_t modifier;
		uint32_t dataspace;
	}
	drm_fourcc;
} egl_linux_pixmap;

typedef struct dummy_display
{
	int width;
	int height;

	struct
	{
		khronos_usize_t byte_stride;
		void *data;
	}
	planes[3];

	/* TODO GPUCORE-16122: We may want to change this to be a mali_tpi_format or remove it altogether. */
	struct
	{
		uint32_t fourcc;
		uint64_t modifier;
		uint32_t dataspace;
	} format;
} dummy_display;

/** Converts a pixmap ID to the corresponding pixmap structure.
 *
 * @param[in] id The pixmap ID to lookup
 *
 * @return A pointer to the pixmap structure or NULL on failure
 */
egl_linux_pixmap* egl_lookup_pixmap_ID_mapping(int32_t id);

/** Creates a pixmap ID from a pixmap structure.
 *
 * @param[in] pixmap
 *
 * @return The pixmap ID or -1 on failure
 */
int32_t egl_create_pixmap_ID_mapping(egl_linux_pixmap* pixmap);

/** Destroy a pixmap ID to pixmap structure mapping. The caller is responsible for
 *  destroying the pixmap structure.
 *
 * @param[in] id The pixmap ID for which the corresponding
 *               mapping should be destroyed
 *
 * @return EGL_TRUE on success or EGL_FALSE on failure
 */
unsigned int egl_destroy_pixmap_ID_mapping(int32_t id);

#endif /* _MALI_FBDEV_TYPES_H_ */
